********************************************************************************
/**THIS FILE CREATES APPENDIX TABLES AND FIGURES FOR 
“The Effects of Education on Financial Outcomes: Evidence from Kenya”**
 
 Additional packages needed: cgmreg cgmwildboot2 ivreg2 xtivreg2 ranktest unique boottest
 */

clear all

// Set your local directory here
cd ""

global dr = "input_data/"
global d0 = "output_data/"
global dtab = "tables_figures"


log using "appendix_tables_figures.do", text replace

** Load in basefile with all variables created **
use "$d0/fpe_FinAccess_basefile.dta", clear


********************************************************************************
****					Table A1: Education Levels 							****
********************************************************************************

preserve

eststo clear
local i = 0
foreach var of varlist some_primary completed_primary some_secondary  {
local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(1211) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)

estadd matrix cgm = A

local label : variable label `var'
local lab`i' = "`label'"
		
}


* 2.2: Create a table where the dependent variable is a dummy for having completed exactly
* the different levels of education in the data. 

local i = 0
foreach var of varlist some_primary_only completed_primary_only some_secondary_only  {
local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29  i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(1211) ///
 null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo d`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29  [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)

estadd matrix cgm = A

local label : variable label `var'
local lab`i' = "`label'"
		
}

label var FPExintensity "`lab1'"

estout a1 d1 using "$dtab/tableA1.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
forv a = 2(1)`i' {

label var FPExintensity "`lab`a''"
	
estout a`a' d`a' using "$dtab/tableA1.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensity)	///
	mlabels(, none) collabels(, none) style(tex) label starlevels(* 0.10 ** 0.05 *** 0.01)
	
}
	
estout a1 d1 using "$dtab/tableA1.tex", append	///
	s(N, labels("Observations") fmt(0)) drop(FPExintensity female age_?? _cons) ///
	mlabels(, none) collabels(, none) style(tex) posthead("[1em]")	

eststo clear
restore

********************************************************************************
****					Table A2: Banking Products							****
********************************************************************************
preserve
keep if sample2 == 1
compress

cap gen var_of_interest = .

eststo clear
local i = 0

foreach var of varlist ever_banked2 banked_only_mobile2 ever_banked_mpesa_only ///
ever_banked has_bank_product2 ever_formal_savings_product has_formal_savings_product ///
ever_form_loan_credit_prod has_form_loan_credit_prod insurance_product {

local i = `i' + 1
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

* First, Reduced Form *
replace var_of_interest = FPExintensity
xi: cgmwildboot2 `var' var_of_interest  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Second, Report the naive OLS results for education on our outcomes first *
replace var_of_interest = educ_yrs_censored
xi: cgmwildboot2 `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
	cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)

eststo b`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Third, Then, instrument for education with the FPExintensity variable *
eststo c`i': xtivreg2 `var' (var_of_interest = FPExintensity) female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) first fe i(sub_region) savefirst savefprefix(_all_)
matrix A = e(first)
local r = rownumb(matrix(A), "APF")
estadd scalar apF = A[`r',1]
if `i' == 1 {
local apf_all = string(round(A[15,1],.01),"%9.2f")
}


local label : variable label `var'
local lab`i' = "`label'"

}


lab var var_of_interest "`lab1'"	
	
estout a1 b1 c1  using "$dtab/tableA2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
forv a = 2(1)3 {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA2.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a1 b1 c1 using "$dtab/tableA2.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")


lab var var_of_interest "`lab4'"

estout a4 b4 c4  using "$dtab/tableA2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

estout a4 b4 c4 using "$dtab/tableA2.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")	

lab var var_of_interest "`lab5'"	
	
estout a5 b5 c5  using "$dtab/tableA2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
forv a = 6(1)`i' {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA2.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a5 b5 c5 using "$dtab/tableA2.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")



	
restore

********************************************************************************
****			Table A3: Financial Security and Retirement					****
********************************************************************************
lab var b2 "Financial literacy"

preserve
keep if sample2 == 1
compress

cap rename has_formal_loan_credit_product has_form_loan_credit_prod
cap rename ever_formal_loan_credit_product ever_form_loan_credit_prod

cap gen var_of_interest = .

***** Financial Security ****
eststo clear
local i = 0

foreach var of varlist b2 numeracy2 any_savings informal_savings_group emergency_funds ///
	safe_place_money   {

local i = `i' + 1
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

* First, Reduced Form *
replace var_of_interest = FPExintensity
xi: cgmwildboot2 `var' var_of_interest  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Second, Report the naive OLS results for education on our outcomes first *
replace var_of_interest = educ_yrs_censored
xi: cgmwildboot2 `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
	cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)

eststo b`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Third, Then, instrument for education with the FPExintensity variable *
eststo c`i': xtivreg2 `var' (var_of_interest = FPExintensity) female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) first fe i(sub_region) savefirst savefprefix(_all_)
matrix A = e(first)
local r = rownumb(matrix(A), "APF")
estadd scalar apF = A[`r',1]
if `i' == 1 {
local apf_all = string(round(A[15,1],.01),"%9.2f")
}

local label : variable label `var'
local lab`i' = "`label'"

}


lab var var_of_interest "`lab1'"

estout a1 b1 c1  using "$dtab/tableA3_financial_security.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

forv a = 2(1)`i' {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA3_financial_security.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a1 b1 c1 using "$dtab/tableA3_financial_security.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")






**** Retirement Plans ****
eststo clear
local i = 0

foreach var of varlist retire_own_plans retire_no_plans retire_safety_net {

local i = `i' + 1
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

* First, Reduced Form *
replace var_of_interest = FPExintensity
xi: cgmwildboot2 `var' var_of_interest  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Second, Report the naive OLS results for education on our outcomes first *
replace var_of_interest = educ_yrs_censored
xi: cgmwildboot2 `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
	cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)

eststo b`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Third, Then, instrument for education with the FPExintensity variable *
eststo c`i': xtivreg2 `var' (var_of_interest = FPExintensity) female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) first fe i(sub_region) savefirst savefprefix(_all_)
matrix A = e(first)
local r = rownumb(matrix(A), "APF")
estadd scalar apF = A[`r',1]
if `i' == 1 {
local apf_all = string(round(A[15,1],.01),"%9.2f")
}

local label : variable label `var'
local lab`i' = "`label'"

}


lab var var_of_interest "`lab1'"

estout a1 b1 c1  using "$dtab/tableA3_retirement.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

forv a = 2(1)`i' {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA3_retirement.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a1 b1 c1 using "$dtab/tableA3_retirement.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")
restore

********************************************************************************
****				Table A4: Shocks Summary Statistics						****
********************************************************************************


cap file close output
file open output using "$dtab/tableA4.tex", write replace

foreach var of varlist had_shock shock_savings shock_borrow shock_sell_assets ///
shock_family_church shock_fundraising shock_other shock_nothing {

local lbl: variable label `var'

su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 1
local fpe_mean = `r(mean)'
local fpe_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & FPE == 0
local nofpe_mean = `r(mean)'
local nofpe_sd = `r(sd)'

foreach l in fpe_mean nofpe_mean fpe_sd nofpe_sd  {
		local `l' = string(round(``l'',.001), "%9.3f")
	}


file write output "`lbl' & " _tab "`fpe_mean' & " _tab "`fpe_sd' & " _tab "`nofpe_mean' & " _tab "`nofpe_sd' \\ " _n
}
count if FPE == 1 &sample2 == 1 & !mi(had_shock)
local N_FPE = `r(N)'
count if FPE == 0 &sample2 == 1 & !mi(had_shock) 
local N_NOFPE = `r(N)'

file write output "\midrule" _n
file write output "Observations & " _tab "\multicolumn{2}{c}{`N_FPE'} & " _tab ///
	"\multicolumn{2}{c}{`N_NOFPE'} \\"  _n


file close output


********************************************************************************
****					Table A5: Dealing with Shocks						****
********************************************************************************
preserve
eststo clear
local i = 0
foreach var of varlist had_shock shock_savings shock_borrow shock_sell_assets shock_family_church ///
shock_fundraising shock_other shock_nothing {

local i = `i' + 1

** Full Sample **
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(415) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)
 
eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 if sample2 == 1   ///
[pw = popwgt_raw], cluster(sub_region) absorb(sub_region)

estadd scalar did_p = A[1,1]

local label : variable label `var'
local lab`i' = "`label'"

}

label var FPExintensity "`lab1'"

estout a1 using "$dtab/tableA5.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p N, layout([@] @) labels(" " "Observations") fmt(3 0)) ///
	keep(FPExintensity)	 style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

label var FPExintensity "`lab2'"

estout a2 using "$dtab/tableA5_2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	

forv a = 3(1)`i' {

label var FPExintensity "`lab`a''"

estout a`a' using "$dtab/tableA5_2.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	mlabels(, none) collabels(, none) style(tex) label starlevels(* 0.10 ** 0.05 *** 0.01)
}

estout a2  using "$dtab/tableA5_2.tex", append	///
	s(N, labels("Observations") fmt(0)) keep( ) ///
	mlabels(, none) collabels(, none) style(tex) posthead("[1em]")

eststo clear
restore

********************************************************************************
****						Table A6: Income								****
****				Table A7: Primary Source of Money						****
********************************************************************************

preserve
keep if sample2 == 1
compress

cap gen var_of_interest = .

* Loop through the two measures of education *
foreach x in /*completed_primary a_14*/ educ_yrs_censored {

eststo clear
local i = 0

foreach var of varlist earned_any_income IHST_earned_income log_earned_income1 ///
log_earned_income2 farming_most_money employed_most_money casual_most_money ///
self_employ_most_money family_friends_most_money other_most_money {

local i = `i' + 1
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

* First, Reduced Form *
replace var_of_interest = FPExintensity
xi: cgmwildboot2 `var' var_of_interest  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Second, Report the naive OLS results for education on our outcomes first *
replace var_of_interest = educ_yrs_censored
xi: cgmwildboot2 `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	i.sub_region [pw = popwgt_raw] if sample2 == 1, ///
	cluster(sub_region) bootcluster(sub_region) reps(999) seed(617) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
matrix A = e(p)

eststo b`i': areg `var' var_of_interest female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A

* Third, Then, instrument for education with the FPExintensity variable *
eststo c`i': xtivreg2 `var' (var_of_interest = FPExintensity) female age_16 age_17 age_18 age_28 age_29 ///
	[pw = popwgt_raw] if sample2 == 1, cluster(sub_region) first fe i(sub_region) savefirst savefprefix(_all_)
matrix A = e(first)
local r = rownumb(matrix(A), "APF")
estadd scalar apF = A[`r',1]
if `i' == 1 {
local apf_all = string(round(A[15,1],.01),"%9.2f")
}


local label : variable label `var'
local lab`i' = "`label'"

}


lab var var_of_interest "`lab1'"	
	
estout a1 b1 c1  using "$dtab/tableA6_income.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
forv a = 2(1)3 {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA6_income.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a1 b1 c1 using "$dtab/tableA6_income.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")


lab var var_of_interest "`lab4'"

estout a4 b4 c4  using "$dtab/tableA6_ln_income.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

estout a4 b4 c4 using "$dtab/tableA6_ln_income.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")	

lab var var_of_interest "`lab5'"	
	
estout a5 b5 c5  using "$dtab/tableA7_money_source.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
forv a = 6(1)`i' {

lab var var_of_interest "`lab`a''"

estout a`a' b`a' c`a'  using "$dtab/tableA7_money_source.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(var_of_interest)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a5 b5 c5 using "$dtab/tableA7_money_source.tex", append	///
	s(N apF, labels("Observations" "F-Stat of Excluded Instrument") fmt("0 2")) keep( ) ///
	mlabels(, none) collabels(, none) style(tex)  prefoot(" & & & \\")


}
	
restore

********************************************************************************
****			Table A8: Summary Statistics by Gender						****
********************************************************************************
lab var FPE "FPE cohort"


cap file close output
file open output using "$dtab/tableA8.tex", write replace

foreach var of varlist educ_yrs_censored some_primary completed_primary ///
	some_secondary age FPE never_attend never_attend_female ///
	never_attend_male fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index ///
	ever_banked2 has_bank_product2 ever_formal_savings_product ///
	b2 numeracy2 retire_own_plans informal_savings_group emergency_funds ///
	safe_place_money any_savings earned_any_income earned_income family_friends_most_money {

preserve
if "`var'" == "never_attend_female" {
keep if female == 1
su `var' [aw=popwgt_raw] if sample2 == 1 & female == 1
local f_mean = `r(mean)'
local f_sd = `r(sd)'
}
else if "`var'" == "never_attend_male" {
keep if female == 0
su `var' [aw=popwgt_raw] if sample2 == 1 & female == 0
local m_mean = `r(mean)'
local m_sd = `r(sd)'
}


else {
su `var' [aw=popwgt_raw] if sample2 == 1 & female == 1
local f_mean = `r(mean)'
local f_sd = `r(sd)'
su `var' [aw=popwgt_raw] if sample2 == 1 & female == 0
local m_mean = `r(mean)'
local m_sd = `r(sd)'
}
local lbl: variable label `var'

if "`var'" != "earned_income" {
foreach l in f_mean f_sd m_mean m_sd {
	local `l' = string(round(``l'',.001), "%9.3f")
}
}
else if "`var'" == "earned_income" {
foreach l in f_mean f_sd m_mean m_sd  {
	local `l' = string(round(``l'',.001), "%9.0f")
}
}

file write output "`lbl' & " _tab "`f_mean' & " _tab "`f_sd' & " _tab "`m_mean' & " _tab "`m_sd' \\ " _n
restore
}
count if female == 1 &sample2 == 1
local N_FPE = `r(N)'
count if female == 0 &sample2 == 1 
local N_NOFPE = `r(N)'

file write output "\midrule" _n
file write output "Observations & " _tab "\multicolumn{2}{c}{`N_FPE'} & " _tab ///
	"\multicolumn{2}{c}{`N_NOFPE'} \\"  _n

file close output

********************************************************************************
****				Table A9: Heterogeneity by Gender						****
********************************************************************************

cap gen FPExintensityxfemale = FPExintensity*female
lab var FPExintensityxfemale "FPE $\times$ Intensity $\times$ Female"

foreach index in kling /*anderson*/ {
local i = 0
foreach var of varlist educ_yrs_censored fin_incl_kling_index fin_capib_kling_index ///
econ_self_suff_kling_index  ever_banked2 has_bank_product2 ever_formal_savings_product earned_any_income{

local i = `i' + 1
********** Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity
replace FPExintensityxfemale = FPExintensity*female

xi: cgmwildboot2 `var' FPExintensityxfemale FPExintensity  female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensityxfemale FPExintensity  female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)
count if e(sample)
local n_a = `r(N)'
estadd matrix cgm = A

lincom _b[FPExintensity]+_b[FPExintensityxfemale]
 local tstat=r(estimate)/r(se)
 local pval = tprob(r(df), abs(`tstat'))
 estadd scalar lincm_p = `pval'

 
**** Use gender-specific intensities ****
replace Intensity = never_attend_female if female == 1
replace Intensity = never_attend_male if female == 0
replace FPExintensity = FPE * Intensity
replace FPExintensityxfemale = FPExintensity*female

xi: cgmwildboot2 `var' FPExintensityxfemale FPExintensity  female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo b`i': areg `var' FPExintensityxfemale FPExintensity  female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)
count if e(sample)
local n_a = `r(N)'
estadd matrix cgm = A

lincom _b[FPExintensity]+_b[FPExintensityxfemale]
 local tstat=r(estimate)/r(se)
 local pval = tprob(r(df), abs(`tstat'))
 estadd scalar lincm_p = `pval' 

 
}


estout a1 a2 a3 a4 a5 a6 a7 a8 using "$dtab/tableA9_1.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensityxfemale FPExintensity female)	///
	order(FPExintensityxfemale FPExintensity female) style(tex) label ///
	mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)


estout b1 b2 b3 b4 b5 b6 b7 b8 using "$dtab/tableA9_2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) keep(FPExintensityxfemale FPExintensity female)	///
	order(FPExintensityxfemale FPExintensity female) style(tex) label ///
	mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

}


********************************************************************************
****				Table A10: Other Potential Outcomes						****
********************************************************************************
preserve
keep if sample2 == 1
compress

eststo clear
local i = 0
foreach var of varlist married ever_married christian muslim other_religion ///
mobile_phone moved internet_usage {
local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)

estadd scalar did_p = A[1,1]

local label : variable label `var'
local lab`i' = "`label'"
		
}

label var FPExintensity "`lab1'"

estout a1 using "$dtab/tableA10.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
	
forv a = 2(1)`i' {

label var FPExintensity "`lab`a''"

estout a`a' using "$dtab/tableA10.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	mlabels(, none) collabels(, none) style(tex) label starlevels(* 0.10 ** 0.05 *** 0.01)
}


estout a1  using "$dtab/tableA10.tex", append	///
	s(N, labels("Observations") fmt(0)) keep( ) ///
	mlabels(, none) collabels(, none) style(tex) posthead("[1em]")

eststo clear
restore

********************************************************************************
****	Table A11: Financial Capability as a Potential Mechanism			****
********************************************************************************
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

eststo clear
local i = 1
foreach var of varlist econ_self_suff_kling_index log_earned_income1 fin_incl_kling_index {

** Baseline **
xi: cgmwildboot2 `var' FPExintensity  female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
local c = A[1,1]
estadd local cgm = "[" + string(round(`c',.001),"%9.3f") + "]"

** Control for Financial Capability **
eststo b`i': areg `var' FPExintensity fin_capib_kling_index female age_16 age_17 age_18 age_28 age_29 ///
	if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) absorb(sub_region)
boottest FPExintensity, nograph seed(617)
estadd local cgm = "[" + string(round(`r(p)',.001),"%9.3f") + "]"
boottest fin_capib_kling_index, nograph seed(617)
estadd local cgm2 = "[" + string(round(`r(p)',.001),"%9.3f") + "]"

local i = `i' + 1
}


estout a1 b1 a2 b2 a3 b3 using "$dtab/tableA11.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) keep(FPExintensity) s(cgm, labels(" "))	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
estout a1 b1 a2 b2 a3 b3 using "$dtab/tableA11.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) keep(fin_capib_kling_index) s(cgm2, labels(" "))	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
estout a1 b1 a2 b2 a3 b3 using "$dtab/tableA11.tex", append  ///
	cells(b(star fmt(3)) se(par fmt(3))) keep( ) s(N r2_a, fmt("0 3") ///
	labels("Observatons" "Adjusted \(R^{2}\)"))	prefoot("\hline") ///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)	
	
********************************************************************************
****				Table A12: Distance to Banking Products					****
********************************************************************************	
preserve
keep if sample2 == 1
compress
	
* Distance to Banking Products *

lab var time_to_bank_branch "Bank branch"
lab var time_to_mpesa "Mobile money agent"
lab var time_to_bank_agent "Bank agent"
lab var time_to_fin_serv_provid "Financial service provider"

lab var dk_time_to_bank_branch "Bank branch"
lab var dk_time_to_mpesa "Mobile money agent"
lab var dk_time_to_bank_agent "Bank agent"
lab var dk_time_to_fin_serv_provid "Financial service provider"

eststo clear
local i = 0
foreach var of varlist time_to_bank_branch time_to_mpesa time_to_bank_agent time_to_fin_serv_provid ///
dk_time_to_bank_branch dk_time_to_mpesa dk_time_to_bank_agent dk_time_to_fin_serv_provid {

local i = `i' + 1
********* Dummy Interacted with Intensity ********
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity

xi: cgmwildboot2 `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 i.sub_region  ///
 if sample2 == 1 [pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo a`i': areg `var' FPExintensity female age_16 age_17 age_18 age_28 age_29 [pw = popwgt_raw] ///
if sample2 == 1, cluster(sub_region) absorb(sub_region)

estadd scalar did_p = A[1,1]

local label : variable label `var'
local lab`i' = "`label'"

}



label var FPExintensity "`lab1'"

estout a1 using "$dtab/tableA12_1.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
	
forv a = 2(1)4 {

label var FPExintensity "`lab`a''"

estout a`a' using "$dtab/tableA12_1.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	mlabels(, none) collabels(, none) style(tex) label starlevels(* 0.10 ** 0.05 *** 0.01)
}

* Don't know Distance to Banking Products *

label var FPExintensity "`lab5'"

estout a5 using "$dtab/tableA12_2.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)
	
forv a = 6(1)8 {

label var FPExintensity "`lab`a''"

estout a`a' using "$dtab/tableA12_2.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) s(did_p, layout([@]) labels(" ") fmt(3)) keep(FPExintensity)	///
	mlabels(, none) collabels(, none) style(tex) label starlevels(* 0.10 ** 0.05 *** 0.01)
}


restore


********************************************************************************
****	Table A13: Heterogeneity by Distance to Mobile Money Agent < 10 minutes	****
********************************************************************************
replace Intensity = never_attend
replace FPExintensity = FPE * Intensity
replace time_to_mpesa = 10 if q3b_2 == 10
replace time_to_mpesa = 11 if q3b_1 == 8

*** MPESA less than 10 minutes away ***
preserve
gen mpesa_close = time_to_mpesa==1 if !mi(time_to_mpesa)
lab var mpesa_close "Mobile money agent within 10 minutes"
gen FPExMpesaxIntensity = mpesa_close*FPE * Intensity
lab var FPExMpesaxIntensity "FPE $\times$ Intensity $\times$ Mobile agent $<$ 10 min."
gen MpesaxIntensity = mpesa_close* Intensity
lab var MpesaxIntensity "Intensity $\times$ Mobile agent $<$ 10 min."
gen FPExMpesa = mpesa_close* FPE
lab var FPExMpesa "FPE $\times$ Mobile agent $<$ 10 min."

eststo clear
keep if sample2 == 1
foreach var of varlist fin_incl_kling_index fin_capib_kling_index econ_self_suff_kling_index {

xi: cgmwildboot2 `var' FPExMpesaxIntensity FPExintensity FPExMpesa MpesaxIntensity  mpesa_close ///
	female age_16 age_17 age_18 age_28 age_29 i.sub_region ///
	[pw = popwgt_raw], cluster(sub_region) bootcluster(sub_region) reps(999) seed(75019) ///
	null(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

matrix A = e(p)	

eststo: areg `var' FPExMpesaxIntensity FPExintensity FPExMpesa MpesaxIntensity  mpesa_close ///
	female age_16 age_17 age_18 age_28 age_29 if sample2 == 1 [pw = popwgt_raw], ///
	cluster(sub_region) absorb(sub_region)
estadd matrix cgm = A
}

estout using "$dtab/tableA13.tex", ///
	keep(FPExMpesaxIntensity FPExintensity FPExMpesa  MpesaxIntensity  mpesa_close)  ///
	replace   starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3)) cgm(par([ ]) fmt(3))) ///
	s(N r2_a, fmt("0 3") ///
	labels("Observations" "Adjusted \(R^{2}\)")) ///
	prefoot("\hline") mlabels(, none) collabels(, none) style(tex) label substitute(X "$\times$")
	
restore	

********************************************************************************
****				Table A14: Separate Cohort Regressions					****
********************************************************************************
local i = 0
foreach var of varlist educ_yrs_censored some_primary completed_primary ///
	some_secondary completed_secondary  fin_incl_kling_index fin_capib_kling_index ///
	econ_self_suff_kling_index {

eststo a`i': areg `var' Intensity  female [pw = popwgt_raw] ///
if FPE == 1 & sample2 ==1, cluster(sub_region) absorb(age)
su `var' if e(sample) [aw=popwgt_raw]
estadd scalar lhsmean = `r(mean)'

boottest Intensity, nograph seed(77450) reps(999)
estadd local cgm = "[" + string(round(`r(p)',.001),"%9.3f") + "]"

eststo b`i': areg `var'  Intensity female [pw = popwgt_raw] ///
	if FPE == 0 & sample2 ==1, cluster(sub_region) absorb(age)
su `var' if e(sample) [aw=popwgt_raw]
estadd scalar lhsmean = `r(mean)'
	
boottest Intensity, nograph seed(77450) reps(999)
estadd local cgm = "[" + string(round(`r(p)',.001),"%9.3f") + "]"

local i = `i' + 1	
	
}	
	

estimates restore a1
lab var Intensity "FPE cohort $(N=`e(N)')$"

estout a0 a1 a2 a3 a4 a5 a6 a7 using "$dtab/tableA14.tex", replace	///
	cells(b(star fmt(3)) se(par fmt(3))) keep(Intensity) s(cgm lhsmean, labels(" " "Dep. Var. Mean"))	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)

estimates restore b1	
lab var Intensity "Older cohort $(N=`e(N)')$"	
	
estout b0 b1 b2 b3 b4 b5 b6 b7 using "$dtab/tableA14.tex", append	///
	cells(b(star fmt(3)) se(par fmt(3))) keep(Intensity) s(cgm lhsmean, labels(" " "Dep. Var. Mean"))	///
	style(tex) label mlabels(,none) collabels(, none) starlevels(* 0.10 ** 0.05 *** 0.01)



********************************************************************************
****	Figure A1: Coefficients on Interactions of Age and FPE Intensity for Education Level				 
****	Figure A2: Coefficients on Interactions of Age and FPE Intensity for Educational Outcomes
****	Figure A3: Coefficients on Interactions of Age and FPE Intensity for Banking Outcomes
****	Figure A4: Coefficients on Interactions of Age and FPE Intensity for Financial Capability Outcomes
****	Figure A5: Coefficients on Interactions of Age and FPE Intensity for Economic Outcomes
********************************************************************************	
gen coef = .
gen coef_ub = .
gen coef_lb = .

gen age_at_fpe = age-12
lab var age_at_fpe "Age in 2003"

* Define upper bound for graphs *
local ub = 28
local ubn = `ub'-1

cap drop IntensityXage_*

forv x = 4(1)27 {
cap gen age_at_fpe`x' = age_at_fpe == `x'
cap gen IntensityXage_`x' = age_at_fpe`x' * Intensity
}

foreach z in a_14 /// Figure A1
	some_primary completed_primary some_secondary /// Figure A2
	ever_banked2 has_bank_product2 ever_formal_savings_product /// Figure A3
	b2 numeracy2 any_savings /// Figure A4
	earned_any_income IHST_earned_income income_independence /// Figure A5
	{

replace coef = 0 if age_at_fpe == 28

cap drop n
bys age: gen n = _n



replace Intensity = never_attend
forv x = 4(1)27 {
replace IntensityXage_`x' = age_at_fpe`x' * Intensity
lab var IntensityXage_`x' "`x'"
}

areg `z' IntensityXage_* age_at_fpe* female [pw = popwgt_raw] if age_at_fpe<=28, cluster(sub_region) absorb(sub_region)

forv x = 4(1)27 {
replace coef = _b[IntensityXage_`x'] if age_at_fpe == `x'
replace coef_ub = _b[IntensityXage_`x'] + 1.96*_se[IntensityXage_`x'] if age_at_fpe == `x'
replace coef_lb = _b[IntensityXage_`x'] - 1.96*_se[IntensityXage_`x'] if age_at_fpe == `x'
}

twoway (connected coef age_at_fpe, lp(solid) lc(black) mc(black)) (line coef_ub age_at_fpe, lp(dash) lc(black)) ///
 (line coef_lb age_at_fpe, lp(dash) lc(black)) if age_at_fpe<=28 & n == 1, graphregion(color(white)) xlab(4(2)28) ///
 legend(off) yline(0, lp(solid) lc(black)) 

	graph export "$dtab/figureAX_`z'.pdf", replace


}

cap log close
